package com.itheima.reggie.common;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.sql.SQLIntegrityConstraintViolationException;

//@ControllerAdvice(annotations = {RestController.class, Controller.class})
//@ResponseBody
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){

//        System.out.println(ex);

        return R.error(ex.getMessage().split(" ")[2]+"已存在");

    }
    @ExceptionHandler(CustomException.class)
    public R<String> exceptionHandler(CustomException ex){

        return R.error(ex.getMessage());

    }
    @ExceptionHandler(IOException.class)
        public R<String> exceptionHandler(IOException ex){
        ex.printStackTrace();
        return R.error("未知错误...");

    }

}
